<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">国家</label>
        <el-input v-model="query.country" clearable placeholder="国家" style="width: 185px;" class="filter-item" @input="crud.toQuery" />
        <label class="el-form-item-label">链接</label>
        <el-input v-model="query.linkUrl" clearable placeholder="链接" style="width: 185px;" class="filter-item" @input="crud.toQuery" />
        <label class="el-form-item-label">是否启用：</label>
        <!-- <el-input v-model="query.enabled" clearable placeholder="状态：1启用、0禁用" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" /> -->
        <el-switch v-model="query.enabled" style="margin-top:-8px;margin-right:10px" :active-value="1" :inactive-value="0" @change="crud.toQuery" />
        <!-- <span>{{query.enabled?'启用':'禁用'}}</span> -->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <el-upload
          slot="right"
          action=""
          accept=".xls,.xlsx"
          :before-upload="beforeFileUpload"
          :http-request="uploadFile"
          class="filter-upload"
          :show-file-list="false"
        >
          <el-button
            type="success"
            size="mini"
            class="filter-item"
            icon="el-icon-upload2"
          >导入</el-button>
        </el-upload>

      </crudOperation>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <!-- <el-form-item label="国家" prop="country">
            <el-input v-model="form.country" style="width: 370px;" />
          </el-form-item> -->
          <el-form-item label="链接" prop="linkUrl">
            <el-input v-model="form.linkUrl" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="country" label="国家" />
        <el-table-column prop="linkUrl" label="链接" />
        <el-table-column v-permission="['admin','goodsLink:edit','goodsLink:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudGoodsLink from '@/api/product/goodsLink'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { Loading } from 'element-ui'

const defaultForm = { linkId: null, country: null, linkUrl: null, enabled: null, description: null, createBy: null, updateBy: null, createTime: null, updateTime: null }
export default {
  name: 'GoodsLink',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '链接',
      url: 'api/goodsLink',
      idField: 'linkId',
      sort: 'linkId,desc',
      enabled: 1,
      crudMethod: { ...crudGoodsLink }})
  },
  data() {
    return {
      file: null,
      permission: {
        add: ['admin', 'goodsLink:add'],
        edit: ['admin', 'goodsLink:edit'],
        del: ['admin', 'goodsLink:del']
      },
      rules: {
        country: [
          { required: true, message: '国家不能为空', trigger: 'blur' }
        ],
        linkUrl: [
          { required: true, message: '链接不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'country', display_name: '国家' },
        { key: 'linkUrl', display_name: '链接' },
        { key: 'enabled', display_name: '状态：1启用、0禁用' }
      ],
      loadingInstance: null
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    beforeFileUpload(file) {
      var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
      console.log(testmsg)
      const extension = testmsg === 'xls'
      const extension2 = testmsg === 'xlsx'
      if (extension || extension2) {
        this.file = file
      } else {
        this.$message({
          message: `只能上传xls或者xlsx文件`,
          type: 'warning'
        })
      }
      return extension || extension2
    },
    uploadFile() {
      if (!this.file) return
      this.loadingInstance = Loading.service({ fullscreen: true, text: '上传文件中...' })
      const formData = new FormData()
      formData.append('uploadFile', this.file)
      crudGoodsLink.uploadFile(formData).then(res => {
        this.loadingInstance && this.loadingInstance.close()
        this.$message({
          message: `本次上传成功${res}条`,
          type: 'success'
        })
        this.crud.page.page = 0
        this.crud.refresh()
      }).catch(err => {
        console.log(err)
        this.loadingInstance && this.loadingInstance.close()
      })
    }
  }
}
</script>

<style scoped>
  .filter-upload{
    display: inline-block;
  }
</style>
